//
//  PainterText.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/17.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterText.h"

using namespace std;
using namespace ytx;

@implementation PainterText

- (NSString *) leftSideTextTransform:(double) value withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    return [NSString stringWithUTF8String:Tools::Convert2FixString(value, indexPosition->getData()->toFix).c_str()];
}

- (CGRect) drawLeftSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Quotation * data = indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    std::vector<std::pair<double, std::string> > leftSideTextList = data->getLeftSideText(startIndex, endIndex, rect, style);
//    double baseValue = leftSideTextList[leftSideTextList.size() / 2].first;
    CGFloat DistanceY = rect.getDistanceY();
    CGPoint point = CGPointMake(rect.getBeginX(), rect.getEndY());
    
    CGRect leftSideTextSize;
    
    for (vector<pair<double, string> >::const_iterator it = leftSideTextList.begin();
         it != leftSideTextList.end();
         ++it, point.y -= DistanceY)
    {
        double value = (*it).first;
        
        NSString * text;
        if (isnan(value)) {
            text = @"";
        }
        else{
            text = [self leftSideTextTransform:value withViewModelIndexPosition:indexPosition];
        }
        
        ChartCanvasTextAnchor anchor;
        
        if (it == leftSideTextList.begin()) {
            anchor = LeftBottom;
        } else if (it + 1 == leftSideTextList.end()) {
            anchor = LeftTop;
        } else {
            anchor = LeftMiddle;
        }
        leftSideTextSize = [ctx fillText:text withX:point.x withY:point.y withColor:[NSString stringWithUTF8String:(*it).second.c_str()] withFont:[ NSString stringWithUTF8String:style.font.c_str() ] withSize:style.size withAnchor:anchor];
    }
    return leftSideTextSize;
}

- (void) drawRightSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{

}

- (void) drawBottomSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    
}

- (void) drawTopSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withDataIndex:(int) index withLeftSideTextRect:(CGRect) leftSideTextRect
{
    Quotation * data = indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    vector<pair<string, string> > topSideTextList = data->getTopSideText(index);
    if (topSideTextList.size() < 1) {
        return;
    }
    
    pair<string, string> infoTitle = data->getTopSideInfoText();
    
    CGFloat x = leftSideTextRect.origin.x + leftSideTextRect.size.width + style.topSideTextLeftMargin;
    
    if(infoTitle.first != ""){
        topSideTextList.insert(topSideTextList.begin(), infoTitle);
    }
    
    NSMutableArray * texts = [NSMutableArray new];
    NSMutableArray * fonts = [NSMutableArray new];
    NSMutableArray * colors = [NSMutableArray new];
    NSMutableArray * sizes = [NSMutableArray new];
    
    NSString * font = [ NSString stringWithUTF8String:style.font.c_str() ];
    
    for (vector<pair<string, string> >::const_iterator it = topSideTextList.begin();
         it != topSideTextList.end(); ++it) {
        if ((*it).first == "") {
            continue;
        }
        [texts addObject:[NSString stringWithUTF8String:((*it).first+" ").c_str()]];
        [colors addObject:[NSString stringWithUTF8String:(*it).second.c_str()]];
        [fonts addObject:font];
        [sizes addObject:@(style.size)];
    }
    // minor [ (12,9,26) DEA:0.08 DIF:0.32 MACD:0.48 ]
    [ctx fillTexts:texts withX:x withY:rect.getBeginY() withColors:colors withFonts:fonts withSizes:sizes withAnchor:LeftTop];
    
}

@end
